package com.imay.live.fragment.discovery;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.imay.live.R;
import com.imay.live.activity.MainActivity2;
import com.imay.live.activity.RoomLiveNormalActivity;
import com.imay.live.activity.discovery.HotLabelActivity;
import com.imay.live.adapter.DiscoveryAdapter;
import com.imay.live.analytics.MobclickAgent;
import com.imay.live.model.FeedModel;
import com.imay.live.model.RoomInfoModel;
import com.imay.live.proto.ErrorResult;
import com.imay.live.proto.Result;
import com.imay.live.proto.helper.ProtoCallBack;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.view.custom.PastseRelatvieLayout2;

import java.util.ArrayList;

import static com.imay.live.proto.ResultCode.CodeSuccess;

/**
 * Created by Administrator on 2017/3/27.
 */

public class HotFragment extends LoadFragment {
    public static final int DATACOUNT = 60;


    public static HotFragment newInstance() {
        return new HotFragment();
    }

    PastseRelatvieLayout2 pastseView;


    public void updateFeedSuggest(ArrayList<FeedModel> feedSuggestModel) {
        super.updateFeedSuggest(feedSuggestModel);
        if (pastseView != null) {
            //pastseView.setModel(feedSuggestModel);
        }
    }

    public void updateLabels(ArrayList<String> hotLabels) {
        this.hotLabels = hotLabels;
        if (pastseView != null) {
            pastseView.setModel(hotLabels);
        }
    }

    @Override
    public void onRefresh() {
        if (getActivity() instanceof MainActivity2) {
            ((MainActivity2) getActivity()).reloadData(true);
        }
    }

    @Override
    public void setTabH(int tabH) {
        super.setTabH(tabH);
        addFooterView(tabH);
    }

    @Override
    public boolean getsetEnableRefresh() {
        if (mLinearRecyclerView != null) {
            RecyclerView.LayoutManager layoutManager = mLinearRecyclerView.getLayoutManager();
            if (layoutManager instanceof LinearLayoutManager) {
                int index = ((LinearLayoutManager) layoutManager).findFirstCompletelyVisibleItemPosition();
                return index == 0;
            }
        }
        return super.getsetEnableRefresh();
    }

    public void onRequestSuccess(String command, String data) {
        try {
            Result<ArrayList<RoomInfoModel>> result = JSON.parseObject(data, new TypeReference<Result<ArrayList<RoomInfoModel>>>() {
            }.getType());
            if (result.result == CodeSuccess && result.data != null) {
                mpapecount = result.data.size();
                if (mpapecount == 0) {
                    loadError(getResources().getString(R.string.str_nodata));
                } else {
                    mAdapter.setNewData(result.data);
                    if (mpapecount > 0) {
                        addFooterView(tabH);
                    }
                    completeLoad(mpapecount == 0);
                }
            } else {
                loadError(getResources().getString(R.string.str_loaderror));
            }

        } catch (Exception e) {
            loadError(getResources().getString(R.string.str_loaderror));
        }
    }

    public void onRequestError(String command, int code, String message) {
        loadError(getResources().getString(R.string.str_loaderror));
    }

//    public void onRefresh() {
//        ProtoRequestHelper.task(ProtoHttpFactory.getRoomSuggest(0, DATACOUNT)).setTag(this.hashCode()).request(new ProtoCallBack() {
//            @Override
//            public void onRequestSuccess(String command, String data) {
//                HotFragment.this.onRequestSuccess(command, data);
//
//            }
//
//            @Override
//            public void onRequestError(String command, int code, String message) {
//                HotFragment.this.onRequestError(command, code, message);
//            }
//        });
//    }

    public void onLoadMore() {
        ProtoRequestHelper.task(ProtoHttpFactory.getRoomSuggest(mpapecount, DATACOUNT)).setTag(this.hashCode()).request(new ProtoCallBack() {
            @Override
            public void onRequestSuccess(String command, String data) {
                try {
                    Result<ArrayList<RoomInfoModel>> result = JSON.parseObject(data, new TypeReference<Result<ArrayList<RoomInfoModel>>>() {
                    }.getType());
                    if (result.result == CodeSuccess && result.data != null) {
                        mpapecount = result.data.size() + mpapecount;
                        mAdapter.addData(result.data);
                        completeLoadmore(result.data.size() == 0);
                    } else {
                        completeLoadmore(true);
                        loadmoreError(ErrorResult.getString(getContext(), result.result));
                    }

                } catch (Exception e) {
                    loadmoreError(getResources().getString(R.string.str_loaderror));
                }
            }

            @Override
            public void onRequestError(String command, int code, String message) {
                loadmoreError(getResources().getString(R.string.str_loaderror));
            }
        });
    }

//    /**
//     * 给底部添加一个白色的区域
//     *
//     * @param tabH
//     */
//    void addFooterView(int tabH) {
//        if (tabH > 0 && mAdapter != null && tabEmptyView == null && mAdapter.getData().size() > 0) {
//            tabEmptyView = new View(getActivity());
//            tabEmptyView.setBackgroundColor(getResources().getColor(R.color.bg_f5));
//            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, tabH);
//            tabEmptyView.setLayoutParams(params);
//            mAdapter.addFooterView(tabEmptyView);
//        }
//    }

    @Override
    void initAdapte() {
        super.initAdapte();
        needmore = true;
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
        mLinearRecyclerView.setLayoutManager(gridLayoutManager);
        mLinearRecyclerView.setHasFixedSize(true);
        mAdapter = new DiscoveryAdapter(null);
        mLinearRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.discover_live_rl_square) {
                    RoomLiveNormalActivity.startActivity(getActivity(), (ArrayList<RoomInfoModel>) adapter.getData(), position);
                }
//                else if (view.getId() == R.id.discover_live_tv_nick) {
//                    RoomInfoModel roomInfoModel = (RoomInfoModel) adapter.getItem(position);
//                    if ((roomInfoModel != null ? roomInfoModel.getUser() : null) != null) {
//                        ProfileActivity.start(getActivity(), roomInfoModel.getUser().getUid());
//                    }
//                }
            }
        });
    }

    @Override
    void oncreate() {
//        mLinearRecyclerView.setBackgroundColor(getResources().getColor(R.color.bg_f5));
        pastseView = new PastseRelatvieLayout2(getActivity());
        pastseView.setModel(hotLabels);
        pastseView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HotLabelActivity.start(getActivity(), hotLabels);
            }
        });
        mAdapter.addHeaderView(pastseView);
        MobclickAgent.onEvent(getActivity(), "find_hot");
    }
}
